from django.db import models
import pyfo

ARCH_CHOICES = (('x86', 'Intel/AMD 32-bit'),
                ('x86-64', 'Intel/AMD 64-bit'),
                ('PPC', 'PowerPC'))

OS_CHOICES = (('AIX', 'AIX'),
              ('CRAY', 'CRAY'),
              ('OSX', 'Darwin'),
              ('Linux', 'Linux'))

SCHEMA = '/Users/rpwagner/Projects/simcat/apps/simcat/schemas/computer.xsd'

class Computer(models.Model):
    name = models.SlugField(unique=True)
    url = models.URLField(null=True,blank=True)
    os = models.CharField(max_length=16, choices=OS_CHOICES,
                          null=True,blank=True)
    arch = models.CharField(max_length=16, choices=ARCH_CHOICES,
                            null=True,blank=True)
    description = models.TextField(null=True,blank=True)

    def __unicode__(self):
        return self.name

    class Admin:
        pass

    class Meta:
	app_label = "simcat"

    def to_xml(self):
        node_list = [('Name', self.name)]

        if self.url:
            node_list.append(('URL', self.url))
        if self.os:
            node_list.append(('OS', self.os))
        if self.arch:
            node_list.append(('Architecture', self.arch))
        if self.description:
            desc = '<![CDATA[' + self.description + ']]>'
            node_list.append(('Description', desc))

        schema_attr = {"xmlns":"http://cadac.sdsc.edu/schema/runtracker"}
        nodes = ('Computer', node_list, schema_attr)

        return pyfo.pyfo(nodes)

class Queue(models.Model):
    computer = models.ForeignKey(Computer)
    name = models.SlugField()
    description = models.TextField(null=True,blank=True)
    max_wall_clock_limit = models.TimeField("Maximum wall clock time", null=True, blank=True)
    max_nodes = models.PositiveIntegerField("Maximum number of nodes", null=True, blank=True)
    max_tasks_per_node = models.PositiveIntegerField("Maximum tasks per node", 
                                                     null=True, blank=True)

    class Admin:
        pass

    class Meta:
	app_label = "simcat"

    def __unicode__(self):
        return self.name

    def to_xml(self):
        node_list = [('Name', self.name),
                     ('Computer',self.computer.name)]
        if self.description:
            desc = '<![CDATA[' + self.description + ']]>'
            node_list.append(('Description', desc))

        schema_attr = {"xmlns":"http://cadac.sdsc.edu/schema/runtracker"}
        nodes = ('Queue', node_list, schema_attr)

        return pyfo.pyfo(nodes)
